/**
 * dialogService 使bootstrapdialog支持ng
 * 这里只封装了show和showmodel,其余方法可以继续封装
 * @author lu.hong 2016-08-26
 * button参数buttons = [{label: 'success',cssClass: 'btn-success',action: function(){}}]
 * label: 'warning','default','primary','danger'
 * cssClass: 'btn-warning','btn-default','btn-primary','btn-danger'
 */
angular.module('app').factory('dialogService',['$compile',function($compile) {
	//import BootstrapDialog from "bootstrap3-dialog";
	var BootstrapDialog = require('bootstrap3-dialog');//弹出框的变量
    var dialogService = {
        show:function(showmodeldata){
        	var titleText = showmodeldata.title;
        	var buttons = showmodeldata.buttons;
        	var messageText = showmodeldata.message;
        	var type = showmodeldata.type;
        	var size = showmodeldata.size;
        	var onshow = showmodeldata.onshow;
			if(!titleText){
    			titleText = BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DEFAULT];
    		}
    		if(!type){
				type = BootstrapDialog.TYPE_PRIMARY;
    		}
    		if(!size){
				size = BootstrapDialog.SIZE_SMALL;
    		}
    		if(size == "SIZE_NORMAL"){
				size = BootstrapDialog.SIZE_NORMAL;
    		}
    		if(!buttons){
    			buttons = [{
	                label: 'default',
	                cssClass: 'btn-default',
	                action: function(dialogItself){
                		//dialogItself.close();
	                }
	            }, {
	                label: 'primary',
	                cssClass: 'btn-primary',
	                action: function(){

	                }
	            }];
    		}
        	BootstrapDialog.show({
				type: type,//TYPE_DEFAULT,TYPE_INFO,TYPE_PRIMARY,TYPE_SUCCESS,TYPE_WARNING,TYPE_DANGER
				size: size,//SIZE_NORMAL,SIZE_SMALL,SIZE_WIDE,SIZE_LARGE
				title: titleText,
				message: messageText,
				onshow: onshow,
				buttons: buttons,
				closeByBackdrop: false,//点背景关闭
				draggable: true//可拖动
			});
        },
        showmodel:function(showmodeldata){
        	var titleText = showmodeldata.title;
        	var url = showmodeldata.url;
        	var buttons = showmodeldata.buttons;
    		var $scope = showmodeldata.ngdata;
    		var type = showmodeldata.type;
    		var size = showmodeldata.size;

    		if(!titleText){
    			titleText = BootstrapDialog.DEFAULT_TEXTS[BootstrapDialog.TYPE_DEFAULT];
    		}
    		if(!type){
				type = BootstrapDialog.TYPE_PRIMARY;
    		}
    		if(!size){
				size = BootstrapDialog.SIZE_SMALL;
    		}
    		if(size == "SIZE_NORMAL"){
				size = BootstrapDialog.SIZE_NORMAL;
    		}
    		if(size == "SIZE_SMALL"){
				size = BootstrapDialog.SIZE_SMALL;
    		}
    		if(!buttons){
    			buttons = [{
	                label: 'default',
	                cssClass: 'btn-default',
	                action: function(){

	                }
	            }, {
	                label: 'primary',
	                cssClass: 'btn-primary',
	                action: function(){

	                }
	            }];
    		}
        	$('<div></div>').load(url,function(html){
				var content = $compile(html)($scope);
				$scope.$apply();
				BootstrapDialog.show({
					type: type,//TYPE_DEFAULT,TYPE_INFO,TYPE_PRIMARY,TYPE_SUCCESS,TYPE_WARNING,TYPE_DANGER
					size: size,//SIZE_NORMAL,SIZE_SMALL,SIZE_WIDE,SIZE_LARGE
					title: titleText,
					message: content,
					buttons: buttons,
					closeByBackdrop: false,//点背景关闭
					draggable: true//可拖动
				});
			});
        }
    }
    return dialogService;
}]);